package com.online.onlineshop.controllers;

import com.online.onlineshop.entity.Comment;
import com.online.onlineshop.services.ForCommentService;
import com.online.onlineshop.utils.ResultForFrontEnd;
import com.online.onlineshop.utils.ReturnData;
import com.online.onlineshop.vo.CommentVo;
import com.online.onlineshop.vo.ProdCommentVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zihao.wang1
 * @date 2022/4/25
 */
@RequestMapping("comment")
@RestController
public class CommentController extends RootController {

    @Resource
    ForCommentService forCommentService;

    @RequestMapping("addComment")
    public ResultForFrontEnd<Integer> addComment(HttpSession session, @RequestBody Comment comment) {
        int uid = getUidFromSession(session);
        String userName = getUserNameFromSession(session);
        Integer res = forCommentService.addComment(comment, uid, userName);
        return new ResultForFrontEnd<>(ok, res);
    }

    @RequestMapping("selectComment")
    public ResultForFrontEnd<Comment> selectComment(@RequestParam Integer oid, @RequestParam Integer pid) {

        Comment data = forCommentService.selectComment(oid, pid);
        return new ResultForFrontEnd<>(ok, data);
    }

    @RequestMapping("updateComment")
    public ResultForFrontEnd<Integer> updateComment(HttpSession session, @RequestBody Comment comment) {
        int uid = getUidFromSession(session);
        String userName = getUserNameFromSession(session);
        Integer res = forCommentService.updateComment(comment, uid, userName);
        return new ResultForFrontEnd<>(ok, res);
    }

    @RequestMapping("selectByComment")
    public ReturnData<List<CommentVo>> selectByComment(@ModelAttribute Comment comment, @RequestParam(defaultValue = "0") int page,
                                                       @RequestParam(defaultValue = "10") int limit, HttpSession session) {
        int uid = getUidFromSession(session);
        return forCommentService.selectByComment(comment, page, limit, uid);
    }

    @RequestMapping("selectCommentByPid")
    public ReturnData<List<ProdCommentVO>> selectCommentByPid(@RequestParam("pid") Integer pid) {
        return forCommentService.selectCommentByPid(pid);
    }

}
